package net.ak.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BaseDao<T> {
	@Autowired
	protected SessionFactory sessionFactory;
	
	/**
	 * This method is used to save or update T.
	 * @param t
	 * @return
	 */
	public boolean saveOrUpdate(T t) {
		boolean success = true;
		Session session = sessionFactory.openSession();
		Transaction tx 	= session.beginTransaction();
		try {
			session.saveOrUpdate(t);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			success = false;
		}
		session.close();
		
		return success;
	}
	
}
